package com.catering.service.impl;

import com.catering.mapper.TableMapper;
import com.catering.pojo.DTable;
import com.catering.service.ITableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 桌位管理服务实现类
 */
@Service
public class TableServiceImpl implements ITableService {
    
    @Autowired
    private TableMapper tableMapper;
    
    @Override
    public List<DTable> findTablesByArea(String areaCode) {
        return tableMapper.selectTablesByArea(areaCode);
    }
    
    @Override
    public Map<String, List<DTable>> findAllAreasWithTables() {
        Map<String, List<DTable>> result = new HashMap<>();
        
        // 获取所有区域代码
        List<String> areaCodes = tableMapper.selectAllAreaCodes();
        
        // 查询每个区域的桌位
        for (String areaCode : areaCodes) {
            List<DTable> tables = tableMapper.selectTablesByArea(areaCode);
            result.put(areaCode, tables);
        }
        
        return result;
    }
    
    @Override
    public List<String> findAllAreas() {
        return tableMapper.selectAllAreaCodes();
    }
    
    @Override
    public DTable findByTableNumber(String tableNumber) {
        return tableMapper.selectByTableNumber(tableNumber);
    }
    
    @Override
    public boolean addTable(DTable table) {
        int rows = tableMapper.insertTable(table);
        return rows > 0;
    }
    
    @Override
    public boolean updateTableStatus(String tableNumber, Integer status) {
        // 先查询桌位是否存在
        DTable table = tableMapper.selectByTableNumber(tableNumber);
        if (table == null) {
            return false;
        }
        
        // 更新状态
        table.setStatus(status);
        int rows = tableMapper.updateTable(table);
        return rows > 0;
    }
    
    @Override
    public boolean deleteTable(String tableNumber) {
        int rows = tableMapper.deleteByTableNumber(tableNumber);
        return rows > 0;
    }
} 